package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Scene;
import cn.wolfcode.trip.base.query.SceneQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.ISceneService;
import cn.wolfcode.trip.base.service.IThemeService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
// 景点的控制器
@Controller
@RequestMapping("/scene")
public class SceneController {

    @Autowired
    private ISceneService sceneService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IThemeService themeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SceneQueryObject qo) {
        model.addAttribute("regions", regionService.list(null));
        model.addAttribute("themes", themeService.list());
        model.addAttribute("pageInfo", sceneService.query(qo));
        return "scene/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Scene entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            entity.setCoverUrl(url);
        }
        sceneService.saveOrUpdate(entity);
        return "redirect:/scene/list.do";
    }
}
